package utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 格式转换工具类
 * Created by cxland on 2017/9/22.
 */
public class FormatUtils {
    /**
     * 下划线转驼峰
     * @param str
     * @return
     */
    public static String lineToHump(String str){
        Pattern linePattern = Pattern.compile("_(\\w)");
        str = str.toLowerCase();
        Matcher      matcher = linePattern.matcher(str);
        StringBuffer sb      = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰转下滑线
     * @param humpString
     * @return
     */
    public static String humpToLine(String humpString) {

        String regexStr = "[A-Z]";
        Matcher matcher = Pattern.compile(regexStr).matcher(humpString);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String g = matcher.group();
            matcher.appendReplacement(sb, "_" + g.toLowerCase());
        }
        matcher.appendTail(sb);
        if (sb.charAt(0) == '_') {
            sb.delete(0, 1);
        }
        return sb.toString();
    }
    /**
     * 转换小写
     * @param string
     * @return
     */
    public static String toLowercase(String string){
        String str=string;
        //用正则表达式判断是否为大写字母
        Pattern p = Pattern.compile("[a-z]+");
        Matcher m = p.matcher(str);
        //如果是大写，则转换为小写，否则转换为大写
        if (m.matches() == false){
            str=str.toLowerCase();
        }else{
            str=str.toUpperCase();
        }
        return str;
    }

    /**
     * 首字母大写
     * @param name
     * @return
     */
    public static String captureName(String name) {
        name = name.substring(0, 1).toUpperCase() + name.substring(1);
        return  name;

    }
}
